package View;

import jade.core.Agent;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import Model.Car;

import map.CellStatus.CellStatusType;
import map.Map;

public class DrawPanel extends JPanel implements MouseListener {
	private static final long serialVersionUID = 1L;

	private Map map;
	static private BufferedImage up;
	static private BufferedImage down;
	static private BufferedImage left;
	static private BufferedImage right;
	@SuppressWarnings("unused")
	static private BufferedImage cross;
	static private BufferedImage red;
	static private BufferedImage green;
	static private BufferedImage block;
	static private BufferedImage blackDOWN;
	static private BufferedImage blackUP;
	static private BufferedImage blackLEFT;
	static private BufferedImage blackRIGHT;
	static private BufferedImage bluDOWN;
	static private BufferedImage bluUP;
	static private BufferedImage bluLEFT;
	static private BufferedImage bluRIGHT;
	static private BufferedImage greenDOWN;
	static private BufferedImage greenUP;
	static private BufferedImage greenLEFT;
	static private BufferedImage greenRIGHT;
	static private BufferedImage grayDOWN;
	static private BufferedImage grayUP;
	static private BufferedImage grayLEFT;
	static private BufferedImage grayRIGHT;
	
	private Point start = null;
	private Point end = null;
	private Agent agent;

	private PanelGui panel;
	public DrawPanel(PanelGui panel,Map map, Agent agent) {
		this.map = map;
		this.agent = agent;
		this.panel = panel;
		this.addMouseListener(this);
		loadImage();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		for (int i = 0; i < map.getSize(); i++) {
			for (int j = 0; j < map.getSize(); j++) {
				if (map.getMap()[i][j].getStatus().equals(CellStatusType.UP)) {
					g.drawImage(up, j * PanelGui.CELL_DIMENSION, i
							* PanelGui.CELL_DIMENSION, PanelGui.CELL_DIMENSION,
							PanelGui.CELL_DIMENSION, null);
				} else if (map.getMap()[i][j].getStatus().equals(
						CellStatusType.DOWN)) {
					g.drawImage(down, j * PanelGui.CELL_DIMENSION, i
							* PanelGui.CELL_DIMENSION, PanelGui.CELL_DIMENSION,
							PanelGui.CELL_DIMENSION, null);
				} else if (map.getMap()[i][j].getStatus().equals(
						CellStatusType.RIGHT)) {
					g.drawImage(right, j * PanelGui.CELL_DIMENSION, i
							* PanelGui.CELL_DIMENSION, PanelGui.CELL_DIMENSION,
							PanelGui.CELL_DIMENSION, null);
				} else if (map.getMap()[i][j].getStatus().equals(
						CellStatusType.LEFT)) {
					g.drawImage(left, j * PanelGui.CELL_DIMENSION, i
							* PanelGui.CELL_DIMENSION, PanelGui.CELL_DIMENSION,
							PanelGui.CELL_DIMENSION, null);
				} else if (map.getMap()[i][j].getStatus().equals(
						CellStatusType.CROSS)) {
					if (map.getCrossRoadsFromPoint(new Point(i, j))
							.isClosedEntry(new Point(i, j)))
						g.drawImage(red, j * PanelGui.CELL_DIMENSION, i
								* PanelGui.CELL_DIMENSION,
								PanelGui.CELL_DIMENSION,
								PanelGui.CELL_DIMENSION, null);
					else
						g.drawImage(green, j * PanelGui.CELL_DIMENSION, i
								* PanelGui.CELL_DIMENSION,
								PanelGui.CELL_DIMENSION,
								PanelGui.CELL_DIMENSION, null);
				} else if (map.getMap()[i][j].getStatus().equals(
						CellStatusType.BLOCK)) {
					g.drawImage(block, j * PanelGui.CELL_DIMENSION, i
							* PanelGui.CELL_DIMENSION, PanelGui.CELL_DIMENSION,
							PanelGui.CELL_DIMENSION, null);
				}
			}
		}
		String color, status;
		for (int i = 0; i < map.getCars().size(); ++i) {
			color = map.getCars().get(i).getColor();
			status = map.getMap()[map.getCars().get(i).getPosition().x][map
					.getCars().get(i).getPosition().y].getStatus().name();
			if (status.equals("CROSS")) {
				try {
					status = map.getCars().get(i).getFromHashMap().name();
				} catch (Exception e) {
					status = "UP";
				}
			}
			g.drawImage(getImage(color + status), map.getCars().get(i)
					.getPosition().y
					* PanelGui.CELL_DIMENSION, map.getCars().get(i)
					.getPosition().x
					* PanelGui.CELL_DIMENSION, PanelGui.CELL_DIMENSION,
					PanelGui.CELL_DIMENSION, null);
		}

	}

	private Image getImage(String string) {
		if (string.equals("blackDOWN"))
			return blackDOWN;
		if (string.equals("blackUP"))
			return blackUP;
		if (string.equals("blackLEFT"))
			return blackLEFT;
		if (string.equals("blackRIGHT"))
			return blackRIGHT;
		if (string.equals("greenDOWN"))
			return greenDOWN;
		if (string.equals("greenUP"))
			return greenUP;
		if (string.equals("greenLEFT"))
			return greenLEFT;
		if (string.equals("greenRIGHT"))
			return greenRIGHT;
		if (string.equals("grayDOWN"))
			return grayDOWN;
		if (string.equals("grayUP"))
			return grayUP;
		if (string.equals("grayLEFT"))
			return grayLEFT;
		if (string.equals("grayRIGHT"))
			return grayRIGHT;
		if (string.equals("bluDOWN"))
			return bluDOWN;
		if (string.equals("bluUP"))
			return bluUP;
		if (string.equals("bluLEFT"))
			return bluLEFT;
		if (string.equals("bluRIGHT"))
			return bluRIGHT;
		return null;
	}

	private void loadImage() {
		try {
			up = ImageIO.read(new File("res/up.png"));
			down = ImageIO.read(new File("res/down.png"));
			left = ImageIO.read(new File("res/left.png"));
			right = ImageIO.read(new File("res/right.png"));
			cross = ImageIO.read(new File("res/cross.png"));
			block = ImageIO.read(new File("res/block.png"));
			blackDOWN = ImageIO.read(new File("res/blackDOWN.png"));
			blackUP = ImageIO.read(new File("res/blackUP.png"));
			blackLEFT = ImageIO.read(new File("res/blackLEFT.png"));
			blackRIGHT = ImageIO.read(new File("res/blackRIGHT.png"));
			bluDOWN = ImageIO.read(new File("res/bluDOWN.png"));
			bluUP = ImageIO.read(new File("res/bluUP.png"));
			bluLEFT = ImageIO.read(new File("res/bluLEFT.png"));
			bluRIGHT = ImageIO.read(new File("res/bluRIGHT.png"));
			greenDOWN = ImageIO.read(new File("res/greenDOWN.png"));
			greenUP = ImageIO.read(new File("res/greenUP.png"));
			greenLEFT = ImageIO.read(new File("res/greenLEFT.png"));
			greenRIGHT = ImageIO.read(new File("res/greenRIGHT.png"));
			grayDOWN = ImageIO.read(new File("res/grayDOWN.png"));
			grayUP = ImageIO.read(new File("res/grayUP.png"));
			grayLEFT = ImageIO.read(new File("res/grayLEFT.png"));
			grayRIGHT = ImageIO.read(new File("res/grayRIGHT.png"));
			red = ImageIO.read(new File("res/red.png"));
			green = ImageIO.read(new File("res/green.png"));

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		try {
			int j = (e.getPoint().x) / PanelGui.CELL_DIMENSION;
			int i = (e.getPoint().y) / PanelGui.CELL_DIMENSION;

			System.out.println("point:[" + i + ":" + j + "]" + " = "
					+ map.getMap()[i][j].getStatus());

	
//			CrossRoads a = map.getPrecNearestCrossRoads(new Point(i, j));
//			CrossRoads b = map.getNextNearestCrossRoads(new Point(i, j));
//			System.out.println(a + " " + b + map.getCrossRoadsGraph().getEdgeWeight(map.getCrossRoadsGraph().getEdge(a, b)));
			List<Car> lisCars = map.getCars();
			for (Car car : lisCars) {
				if (car.getPosition().equals(new Point(i, j)))
					System.out.println("Start " + car.getSource()
							+ " Destination " + car.getDestination());
			}
			
			if(start != null && end != null){
				start = null;
				panel.getSourceLabel().setText("click on a map");
				panel.getDestinationLabel().setText("click on a map");
				end = null;
			}
			
			if(start == null){
				start = new Point(i, j);
				panel.getSourceLabel().setText("["+i+":"+j+"]");
			}else if(start != null && end == null){
//				System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
				end = new Point(i, j);
				panel.getDestinationLabel().setText("["+i+":"+j+"]");
//				System.out.println("FROM "+ start + " TO " + end);
				if(!map.getPointsListFrom(start, end, new HashMap<Point, CellStatusType>()).isEmpty()){
					agent.addBehaviour(new AddCar(start,end, map));
				}
//				System.out.println(">>>>>><<<<<<<<<<<<<<<<<<<<<<");
			} 
			
		} catch (Exception ec) {
			// TODO: handle exception
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

}
